package com.ruoyi.binance.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.binance.domain.BinanceSimulateConf;
import com.ruoyi.binance.mapper.BinanceSimulateConfMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.binance.mapper.BinanceSimulateOrderMapper;
import com.ruoyi.binance.domain.BinanceSimulateOrder;
import com.ruoyi.binance.service.IBinanceSimulateOrderService;

/**
 * 模拟交易订单Service业务层处理
 * 
 * @author zq
 * @date 2025-09-22
 */
@Service
public class BinanceSimulateOrderServiceImpl implements IBinanceSimulateOrderService 
{
    @Autowired
    private BinanceSimulateOrderMapper binanceSimulateOrderMapper;
    @Autowired
    private BinanceSimulateConfMapper binanceSimulateConfMapper;
    /**
     * 查询模拟交易订单
     * 
     * @param id 模拟交易订单主键
     * @return 模拟交易订单
     */
    @Override
    public BinanceSimulateOrder selectBinanceSimulateOrderById(Long id)
    {
        return binanceSimulateOrderMapper.selectBinanceSimulateOrderById(id);
    }

    /**
     * 查询模拟交易订单列表
     * 
     * @param binanceSimulateOrder 模拟交易订单
     * @return 模拟交易订单
     */
    @Override
    public List<BinanceSimulateOrder> selectBinanceSimulateOrderList(BinanceSimulateOrder binanceSimulateOrder)
    {
        return binanceSimulateOrderMapper.selectBinanceSimulateOrderList(binanceSimulateOrder);
    }

    /**
     * 新增模拟交易订单
     * 
     * @param binanceSimulateOrder 模拟交易订单
     * @return 结果
     */
    @Override
    public int insertBinanceSimulateOrder(BinanceSimulateOrder binanceSimulateOrder)
    {
        binanceSimulateOrder.setCreateTime(DateUtils.getNowDate());
        return binanceSimulateOrderMapper.insertBinanceSimulateOrder(binanceSimulateOrder);
    }

    /**
     * 修改模拟交易订单
     * 
     * @param binanceSimulateOrder 模拟交易订单
     * @return 结果
     */
    @Override
    public int updateBinanceSimulateOrder(BinanceSimulateOrder binanceSimulateOrder)
    {
        binanceSimulateOrder.setUpdateTime(DateUtils.getNowDate());
        return binanceSimulateOrderMapper.updateBinanceSimulateOrder(binanceSimulateOrder);
    }

    /**
     * 批量删除模拟交易订单
     * 
     * @param ids 需要删除的模拟交易订单主键
     * @return 结果
     */
    @Override
    public int deleteBinanceSimulateOrderByIds(Long[] ids)
    {
        return binanceSimulateOrderMapper.deleteBinanceSimulateOrderByIds(ids);
    }

    /**
     * 删除模拟交易订单信息
     * 
     * @param id 模拟交易订单主键
     * @return 结果
     */
    @Override
    public int deleteBinanceSimulateOrderById(Long id)
    {
        return binanceSimulateOrderMapper.deleteBinanceSimulateOrderById(id);
    }

    @Override
    public void orderSuccessResult(BinanceSimulateOrder order) {
        //更新订单状态
        order.setStatus("FILLED");
        binanceSimulateOrderMapper.updateBinanceSimulateOrderByOrderId(order);
        //更新交易配置
        BinanceSimulateConf conf = binanceSimulateConfMapper.selectBinanceSimulateConfById(order.getConfigId());
        if (conf != null){
            if("BUY".equals(order.getSide())){
                conf.setSide("BUY");
                conf.setUsdtNumber(conf.getUsdtNumber().subtract(order.getQuoteOrderQty()));
                conf.setCoinNumber(order.getQuoteOrderQty().divide(order.getPrice(), 8, RoundingMode.HALF_UP));
                conf.setCostPrice(order.getPrice());
                binanceSimulateConfMapper.updateBinanceSimulateConf(conf);
            }
            if("SELL".equals(order.getSide())){
                conf.setSide("SELL");
                conf.setUsdtNumber(order.getPrice().multiply(order.getQuantity()));
                conf.setCoinNumber(conf.getCoinNumber().subtract(order.getQuantity()));
                conf.setUsdtProfit(conf.getUsdtProfit().add(conf.getUsdtNumber().subtract(conf.getCostPrice().multiply(order.getQuantity()))));
                binanceSimulateConfMapper.updateBinanceSimulateConf(conf);
            }
        }
    }
}
